package com.itheima.web.controller.system;


import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Dept;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


// uri:/system/dept?operation=list
@WebServlet("/system/dept")
public class DeptServlet extends BaseServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String operation = request.getParameter("operation");
            if ("list".equals(operation)) {
                //查询所有
                this.findAll(request, response);
            } else if ("toAdd".equals(operation)) {
                //跳转添加页面
                this.toAdd(request, response);
            } else if ("save".equals(operation)) {
                //添加信息
                this.save(request, response);
            } else if ("toEdit".equals(operation)) {
                //跳转编辑页面
                this.toEdit(request, response);
            } else if ("edit".equals(operation)) {
                //保存修改信息
                this.edit(request, response);
            } else if ("delete".equals(operation)) {
                //删除信息
                this.delete(request, response);
            }
        }

        private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
            //DeptService deptService =new DeptServiceImpl();
            Dept dept = BeanUtil.fillBean(request, Dept.class);
            deptService.delete(dept);
            response.sendRedirect(request.getContextPath() + "/system/dept?operation=list");
        }

        //保存编辑信息
        private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
            //DeptService deptService =new DeptServiceImpl();
            Dept dept = BeanUtil.fillBean(request, Dept.class, "yyyy-MM-dd");
            deptService.update(dept);
            response.sendRedirect(request.getContextPath() + "/system/dept?operation=list");
        }

        //跳转编辑页面
        private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String id = request.getParameter("id");
            //DeptService deptService = new DeptServiceImpl();
            Dept dept = deptService.findById(id);
            List<Dept> all = deptService.findAll();
            request.setAttribute("deptList", all);
            request.setAttribute("dept", dept);
            request.getRequestDispatcher("/WEB-INF/pages/system/dept/update.jsp").forward(request, response);
        }


        //添加信息
        private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //DeptService deptService =new DeptServiceImpl();
            Dept dept = BeanUtil.fillBean(request, Dept.class, "yyyy-MM-dd");
            deptService.save(dept);
            response.sendRedirect(request.getContextPath() + "/system/dept?operation=list");


        }

        //跳转添加页面
        private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            List<Dept> all = deptService.findAll();
            request.setAttribute("deptList", all);
            request.getRequestDispatcher("/WEB-INF/pages/system/dept/add.jsp").forward(request, response);
        }


        //查询所有
        private void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //DeptService deptService =new DeptServiceImpl();
            int page = 1;
            int size = 5;
            if (StringUtils.isNotBlank(request.getParameter("page"))) {
                page = Integer.parseInt(request.getParameter("page"));
            }
            if (StringUtils.isNotBlank(request.getParameter("size"))) {
                size = Integer.parseInt(request.getParameter("size"));
            }
            PageInfo all = deptService.findAll(page, size);
            request.setAttribute("page", all);
            request.getRequestDispatcher("/WEB-INF/pages/system/dept/list.jsp").forward(request, response);
        }


        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doGet(request, response);
        }
}
